﻿Option Explicit On
Option Strict On

Imports ACOMDirector

Public Class ConfigurationDialog

#Region " Constructor "

    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim AvailableCOMPorts() As String = Framework.RS232.RS232Port.GetPortNames
        Dim SortedCOMPorts As New SortedList(Of Integer, String)
        For Each COMPort As String In AvailableCOMPorts
            Dim ComPortNumberString As String = COMPort.Replace("COM", "").Trim
            If ComPortNumberString = "" Then Continue For
            If Not IsNumeric(ComPortNumberString) Then Continue For
            Dim ComPortNumber As Integer = CInt(ComPortNumberString)

            If Not SortedCOMPorts.ContainsKey(ComPortNumber) Then
                SortedCOMPorts.Add(CInt(COMPort.Replace("COM", "")), COMPort)
            End If
        Next
        cboCOMPort.BeginUpdate()
        For Each item As KeyValuePair(Of Integer, String) In SortedCOMPorts
            cboCOMPort.Items.Add(item.Value)
        Next
        cboCOMPort.EndUpdate()


        cboPowerOnMechanism.BeginUpdate()
        cboPowerOnMechanism.Items.Add("DTR ON")
        cboPowerOnMechanism.Items.Add("RTS ON")
        cboPowerOnMechanism.Items.Add("DTR & RTS ON")
        cboPowerOnMechanism.EndUpdate()

    End Sub

#End Region

#Region " Form Events "

    Private Sub ConfigurationDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadConfiguration()
    End Sub

#End Region

#Region " Private Methods "

    Private Sub LoadConfiguration()

        cboCOMPort.SelectedItem = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.ACOM2000COMPort
        Select Case CType(ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemotePowerOnMechanism, Framework.Hardware.RemotePowerOnSystem)
            Case Framework.Hardware.RemotePowerOnSystem.DTR
                cboPowerOnMechanism.SelectedItem = "DTR ON"
            Case Framework.Hardware.RemotePowerOnSystem.RTS
                cboPowerOnMechanism.SelectedItem = "RTS ON"
            Case Framework.Hardware.RemotePowerOnSystem.RTSandDTR
                cboPowerOnMechanism.SelectedItem = "DTR & RTS ON"
        End Select
        chkRemotePowerOnEnabled.Checked = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.SupportsRemotePowerOn
        chkDetermineOperateMode.Checked = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.DetermineOperateModeOnConnection
        txtNetworkPort.Text = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionPort.ToString()
        txtRemoteAddress.Text = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionAddress
        chkAllowRemoteConnections.Checked = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.AllowRemoteConnections
        chkShowControlPanelOnStartup.Checked = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.ShowControlPanelOnStartup

        Select Case ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionMethod
            Case "TCP"
                optTCP.Checked = True
            Case "HTTP"
                optHTTP.Checked = True
            Case Else
                optTCP.Checked = True
        End Select

        If ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.UseRemoteConnection Then
            optRemote.Checked = True
        Else
            optLocal.Checked = True
        End If

        chkLogger32Enabled.Checked = ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.Logger32InterfaceEnabled
        If ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.Logger32RadioNumber = 2 Then
            optLogger32Radio2.Checked = True
        Else
            optLogger32Radio1.Checked = True
        End If

    End Sub

    Private Function SaveConfiguration() As Boolean

        If txtNetworkPort.Enabled Then
            If Not IsNumeric(txtNetworkPort.Text) Or txtNetworkPort.Text.Trim = "" Then
                MsgBox("You must enter a numeric value for the Remote Port", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Warning")
                Return False
            Else
                ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionPort = CInt(txtNetworkPort.Text)
            End If
        End If

        If optRemote.Checked Then
            If txtRemoteAddress.Text.Trim = "" Then
                MsgBox("You must enter a remote address", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Warning")
                Return False
            Else
                ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionAddress = txtRemoteAddress.Text
            End If
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.UseRemoteConnection = True
        Else
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.UseRemoteConnection = False
        End If

        If cboCOMPort.SelectedItem Is Nothing OrElse String.IsNullOrEmpty(cboCOMPort.SelectedItem.ToString) Then
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.ACOM2000COMPort = ""
        Else
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.ACOM2000COMPort = cboCOMPort.SelectedItem.ToString
        End If

        ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.SupportsRemotePowerOn = chkRemotePowerOnEnabled.Checked
        If chkRemotePowerOnEnabled.Checked Then
            Select Case cboPowerOnMechanism.SelectedItem.ToString()
                Case "DTR ON"
                    ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemotePowerOnMechanism = CInt(Framework.Hardware.RemotePowerOnSystem.DTR)
                Case "RTS ON"
                    ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemotePowerOnMechanism = CInt(Framework.Hardware.RemotePowerOnSystem.RTS)
                Case "DTR & RTS ON"
                    ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemotePowerOnMechanism = CInt(Framework.Hardware.RemotePowerOnSystem.RTSandDTR)
            End Select
        End If

        If optTCP.Checked Then
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionMethod = "TCP"
        ElseIf optHTTP.Checked Then
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.RemoteConnectionMethod = "HTTP"
        End If

        ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.DetermineOperateModeOnConnection = chkDetermineOperateMode.Checked
        ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.AllowRemoteConnections = chkAllowRemoteConnections.Checked
        ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.ShowControlPanelOnStartup = chkShowControlPanelOnStartup.Checked

        ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.Logger32InterfaceEnabled = chkLogger32Enabled.Checked
        If optLogger32Radio2.Checked Then
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.Logger32RadioNumber = 2
        Else
            ACOMDirector.Configuration.SystemConfiguration.Instance.ACOMConfiguration.Logger32RadioNumber = 1
        End If

        ACOMDirector.Configuration.SystemConfiguration.Instance.Save()

        Return True

    End Function

    Private Sub UpdateEnabledItems()

        If optLocal.Checked Then
            txtRemoteAddress.Enabled = False
            cboCOMPort.Enabled = True
            chkRemotePowerOnEnabled.Enabled = True
            cboPowerOnMechanism.Enabled = True
            chkAllowRemoteConnections.Enabled = True
            chkDetermineOperateMode.Enabled = True
            optTCP.Enabled = False
            optHTTP.Enabled = False
        Else
            txtRemoteAddress.Enabled = True
            cboCOMPort.Enabled = False
            chkRemotePowerOnEnabled.Enabled = False
            cboPowerOnMechanism.Enabled = False
            chkAllowRemoteConnections.Enabled = False
            chkDetermineOperateMode.Enabled = False
            optTCP.Enabled = True
            optHTTP.Enabled = False
        End If

        If chkAllowRemoteConnections.Checked Or optRemote.Checked Then
            optTCP.Enabled = True
            optHTTP.Enabled = False
            txtNetworkPort.Enabled = True
        Else
            optTCP.Enabled = False
            optHTTP.Enabled = False
            txtNetworkPort.Enabled = False
        End If

        If chkLogger32Enabled.Checked Then
            optLogger32Radio1.Enabled = True
            optLogger32Radio2.Enabled = True
        Else
            optLogger32Radio1.Enabled = False
            optLogger32Radio2.Enabled = False
        End If

    End Sub

#End Region

#Region " Event Handlers "

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        If SaveConfiguration() Then
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub chkRemotePowerOnEnabled_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRemotePowerOnEnabled.CheckedChanged
        If chkRemotePowerOnEnabled.Checked Then
            cboPowerOnMechanism.Enabled = True
        Else
            cboPowerOnMechanism.Enabled = False
        End If
    End Sub

    Private Sub optRemote_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles optRemote.CheckedChanged
        UpdateEnabledItems()
    End Sub

    Private Sub optLocal_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles optLocal.CheckedChanged
        UpdateEnabledItems()
    End Sub

    Private Sub chkAllowRemoteConnections_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAllowRemoteConnections.CheckedChanged
        UpdateEnabledItems()
    End Sub

    Private Sub chkLogger32Enabled_CheckedChanged(sender As Object, e As EventArgs) Handles chkLogger32Enabled.CheckedChanged
        UpdateEnabledItems()
    End Sub

#End Region

    
End Class